//--------------------Sonic CD UFO Script---------------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
private alias Object.Value0 : Object.ZPos
private alias Object.Value1 : Object.XVelocity
private alias Object.Value2 : Object.ZVelocity
private alias Object.Value3 : Object.Timer
private alias Object.Value4 : Object.ScreenXPos // Difference from the center of the camera (which is where Sonic is always located)
private alias Object.Value5 : Object.ScreenDepth
private alias Object.Value6 : Object.PositionZ

// HUD Alias
private alias Object.Value0 : HUD.UFOsCount       // Initially set via a loop in the UFO script
private alias Object.Value1 : HUD.TimeSeconds     // Displayed time
private alias Object.Value3 : HUD.Rings
private alias Object.Value4 : HUD.LastUFOType     // Used by UFOs for ring streak bonus
private alias Object.Value5 : HUD.SpeedShoesTimer // Used to transmit between player and UFO
private alias Object.Value6 : HUD.BonusUFONo      // "No" akin to Entity No, not as in "is there no UFO?"



// UFOPowerUp Aliases
private alias Object.Value0 : UFOPowerUp.ExplosionDir
private alias Object.Value1 : UFOPowerUp.ExplosionXVel
private alias Object.Value2 : UFOPowerUp.ExplosionYVel
private alias Object.Value4 : UFOPowerUp.YVelocity
private alias Object.Value5 : UFOPowerUp.ScreenDepth
private alias Object.Value7 : UFOPowerUp.StartResults

// SFX Aliases
private alias 1 : SFX_G_RING

// Property Value
private alias 0 : UFO_CONTENTS_RINGS
private alias 1 : UFO_CONTENTS_SPEEDSHOES
private alias 2 : UFO_CONTENTS_TIME

// Priority
private alias 1 : PRIORITY_ACTIVE

public value PlayerHealth = 3;

event ObjectMain

	if PlayerHealth == 0
		object.type = TypeName[Blank Object]
	end if
	
	// Check player interaction in this (rather lengthy) process
	
	// X difference
	temp0 = Object.XPos
	temp0 -= TileLayer[0].XPos
	temp0 >>= 8
	
	// Z mutliplier
	temp1 = Object.ZPos
	temp1 -= TileLayer[0].ZPos
	temp1 >>= 8
	
	// Calculating the X difference
	Sin(temp2, TileLayer[0].Angle)
	temp2 *= temp1
	Cos(temp3, TileLayer[0].Angle)
	temp3 *= temp0
	Object.ScreenXPos = temp2
	Object.ScreenXPos -= temp3
	Object.ScreenXPos >>= 9
	
	// Calculating the Z difference
	Cos(temp2, TileLayer[0].Angle)
	temp2 *= temp1
	Sin(temp3, TileLayer[0].Angle)
	temp3 *= temp0
	Object.ScreenDepth = temp2
	Object.ScreenDepth += temp3
	Object.ScreenDepth >>= 9
	
	//hunting code

	Object.XPos += Object.XVelocity
	Object.ZPos += Object.ZVelocity
	Object.YPos += Object.Yvel

	//title card gone?
	if Object[20].Type == TypeName[Blank Object]	

	// Long list of checks for the hitbox detection

	
	// The Player has to be more than 54 pixels off the ground, but lower than 122 pixels towards the sky
	foreach (TypeName[Sonic], arrayPos2, ALL_ENTITIES)	

		//if player isnt hurt
		if object[2].state != 14

			CheckGreater(object.xpos, object[arrayPos2].xpos)
			temp0 = checkResult
			if temp0 == 1		//right
				if Object.XVelocity > 0
					Object.XVelocity -= 0x3000	//right
					if Object.XVelocity > 0x26000
					Object.XVelocity = 0x26000
					end if
				else
					Object.XVelocity -= 0x1500	//left?? i have no clue
					if Object.XVelocity > 0x26000
					Object.XVelocity = 0x26000
					end if			
				end if
			else	//left
				if Object.XVelocity < 0
					Object.XVelocity += 0x3000
					if Object.XVelocity > 0x26000
					Object.XVelocity = 0x26000
					end if		
				else
					Object.XVelocity += 0x1500
					if Object.XVelocity > 0x26000
					Object.XVelocity = 0x26000
					end if								
				end if
			end if

			CheckGreater(object.ZPos, object[arrayPos2].Value0)
			temp0 = checkResult
			if temp0 == 1		//right
				if Object.ZVelocity > 0
					Object.ZVelocity -= 0x3000	//right
					if Object.ZVelocity > 0x26000
					Object.ZVelocity = 0x26000
					end if
				else
					Object.ZVelocity -= 0x1500	//left?? i have no clue
					if Object.ZVelocity > 0x26000
					Object.ZVelocity = 0x26000
					end if			
				end if
			else	//left
				if Object.ZVelocity < 0
					Object.ZVelocity += 0x3000
					if Object.ZVelocity > 0x26000
					Object.ZVelocity = 0x26000
					end if		
				else
					Object.ZVelocity += 0x1500
					if Object.ZVelocity > 0x26000
					Object.ZVelocity = 0x26000
					end if								
				end if
			end if
		else
			Object.ZVelocity = 0x1500
			Object.XVelocity = 0x1500
		end if

		
		// The UFO has to be within the 2100 range from the player, who's at a depth of 0x5800 normally
		if Object.ScreenDepth < 0x8C8
			if Object.ScreenDepth > 0x094

				// The UFO has to be between -0.125 and +0.125 pixels away from the screen centre
				if Object.ScreenXPos > -0x2000
					if Object.ScreenXPos < 0x2000

						if object[2].value13 <= 0
							object[2].state = 14
						end if

					end if
				end if
			end if
		end if
	next

	end if




end event


event ObjectDraw
	if Object.ScreenDepth > 256 // Don't draw if the UFO is too close to the screen
		
		// Do some maths to find the position & scale the UFO should be at
		
		Object.Scale = 0xC00000
		Object.Scale /= Object.ScreenDepth
		
		temp0 = 768
		temp0 -= Object.Scale
		temp0 /= 6
		
		temp1 = Object.ScreenDepth
		temp1 >>= 12
		
		if temp0 > 0
			Object.Scale += temp0
			Object.Scale -= temp1
		end if
		
		temp0 = Object.ScreenXPos
		temp0 *= 192
		temp0 /= Object.ScreenDepth
		temp0 += Screen.xcenter
		
		temp1 = TileLayer[0].YPos
		temp1 >>= 8
		temp1 -= 0x2000
		temp1 *= 96
		temp1 /= Object.ScreenDepth
		temp1 += 128
		
		// Draw the UFO now
		DrawSpriteScreenFX(0, FX_SCALE, temp0, temp1)
		
		// Find the ground position
		temp1 = TileLayer[0].YPos
		temp1 >>= 8
		temp1 *= 96
		temp1 /= Object.ScreenDepth
		temp1 += 128
		
		// Draw the shadow on the ground
		DrawSpriteScreenFX(3, FX_SCALE, temp0, temp1)
		
	end if

	
end event


event ObjectStartup
	
	LoadSpriteSheet("3D_Test/Objects.gif")
	
	// UFO Frames
	
	// 0 - SLENDERMAN!!
	SpriteFrame(-30, -82, 63, 112, 339, 1)
	
	// 1 - Speed Shoes Variant
	SpriteFrame(-40, -28, 80, 56, 82, 1)
	
	// 2 - Extra Time Variant
	SpriteFrame(-40, -28, 80, 56, 163, 1)
	
	// 3 - UFO Shadow Frame
	SpriteFrame(-40, -8, 80, 16, 1, 117)

	
	// Place a Sonic object into reserved object slot 2 and initialise its values
	Object[6].Type = TypeName[UFO]
	
	// Make Sonic always active
	Object[6].Priority = PRIORITY_ACTIVE
	
	
	// Cycle through all UFO objects in the scene and set them up
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[UFO]
			
			// Due to the difference between editor and game, turn YPos into ZPos here
			Object[6].ZPos = Object[ArrayPos0].YPos


			ResetObjectEntity(ArrayPos0, TypeName[Blank Object], 0, 0, 0)
		
		end if
		
		ArrayPos0++
	loop
	
end event


// ========================
// Editor events
// ========================

event RSDKEdit
	if Editor.ReturnVariable == true
		switch Editor.VariableID
		case EDIT_VAR_PROPVAL // Property Value
			CheckResult = Object.PropertyValue
			break
		case 0 // Contents
			CheckResult = Object.PropertyValue
			break
		end switch
	else
		switch Editor.VariableID
		case EDIT_VAR_PROPVAL // Property Value
			Object.PropertyValue = Editor.VariableValue
			break
		case 0 // Contents
			Object.PropertyValue = Editor.VariableValue
			break
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(Object.PropertyValue)
	
	if Editor.ShowGizmos == true
		// Draw arrows to show the network of UFO nodes
		
		Editor.DrawingOverlay = true
		
		ArrayPos1 = Object.entityPos
		ArrayPos0 = Object.entityPos
		ArrayPos0++
		
		// TODO: change this to "UFO Node" once GetObjectType works with spaces
		GetObjectType(temp0, "UFONode")
		
		while Object[ArrayPos0].Type == temp0
			DrawArrow(Object[ArrayPos1].XPos, Object[ArrayPos1].ZPos, Object[ArrayPos0].XPos, Object[ArrayPos0].ZPos, 255, 255, 0)
			ArrayPos0++
			ArrayPos1++
		loop
		
		ArrayPos0 = Object.entityPos
		ArrayPos0++
		if ArrayPos1 != Object.entityPos
			DrawArrow(Object[ArrayPos1].XPos, Object[ArrayPos1].ZPos, Object[ArrayPos0].XPos, Object[ArrayPos0].ZPos, 255, 255, 0)
		end if
		
		Editor.DrawingOverlay = false
	end if
end event


event RSDKLoad
	LoadSpriteSheet("3D_Test/Objects.gif")
	SpriteFrame(-40, -28, 80, 56, 1, 1)
	SpriteFrame(-40, -28, 80, 56, 82, 1)
	SpriteFrame(-40, -28, 80, 56, 163, 1)

	AddEditorVariable("Contents")
	SetActiveVariable("Contents")
	AddEnumVariable("10 Rings", UFO_CONTENTS_RINGS)
	AddEnumVariable("Speed Shoes", UFO_CONTENTS_SPEEDSHOES)
	AddEnumVariable("Extra Time", UFO_CONTENTS_TIME)
end event
